﻿import com.chumby.util.Delegate;
import com.chumby.util.xml.XmlUtil;
import com.gskinner.events.GDispatcher;

class com.chumby.WidgetParams
{
	private var _configURL:String;
	private var _uploadXML:XML;
	private var _downloadXML:XML;
	private var _loadedParams:Object;
	
	public static var SENT_PARAMS:Number = 1;
	public static var GOT_PARAMS:Number = 2;
	
	public var addEventListener:Function;
	public var removeEventListener:Function;
	private var dispatchEvent:Function;
	
	//--------------------------------------------------------------------------------
	function WidgetParams(url:String)
	{
		GDispatcher.initialize(this);
		if(url != undefined) {
			_configURL = url;
		}
		else if (_root._chumby_instance_url != undefined) {
			_configURL = _root._chumby_instance_url;
		}
		else if (_root._chumby_widget_instance_href != undefined) {
			_configURL = _root._chumby_widget_instance_href;
		}
		trace("Widget Params URL = " + _configURL);
	}
	
	//--------------------------------------------------------------------------------
	/* format of the XML data to post with name-value pairs:
		<widget_instance>
			<widget_parameters>
				<widget_parameter><name>z_val</name><value>333</value></widget_parameter>
				<widget_parameter><name>y_val</name><value>222</value></widget_parameter>
				<widget_parameter><name>x_val</name><value>111</value></widget_parameter>
			</widget_parameters>
		</widget_instance>
	*/

	//--------------------------------------------------------------------------------
	public function sendWidgetParams(parameters:Object)
	{
		var nParams:Number = 0;
		_uploadXML = new XML();
		_uploadXML.onLoad = Delegate.create(this, sentParams);
		
		var widgetParameters:XMLNode = _uploadXML.createElement('widget_parameters');
		for (var var_name:String in parameters) 
		{
			var paramNode:XMLNode = _uploadXML.createElement('widget_parameter');
			var name_xml:XMLNode = _uploadXML.createElement('name');
			name_xml.appendChild(_uploadXML.createTextNode(var_name));
			var value_xml:XMLNode = _uploadXML.createElement('value');
			value_xml.appendChild(_uploadXML.createTextNode(parameters[var_name]));
			paramNode.appendChild(name_xml);
			paramNode.appendChild(value_xml);
			widgetParameters.appendChild(paramNode);
			nParams++;
		}
		var widgetInstance:XMLNode = _uploadXML.createElement('widget_instance');
		widgetInstance.appendChild(widgetParameters);
		_uploadXML.appendChild(widgetInstance);
		trace("--- sending " + nParams + " parameters --- " );
		///trace(_uploadXML.toString());
		_uploadXML.sendAndLoad(_configURL, _uploadXML);
	}

	//--------------------------------------------------------------------------------
	private function sentParams(success:Boolean)
	{
		trace("sentParams " + success);
		//trace(_uploadXML);
		var eventObj:Object = new Object();
		eventObj.type = WidgetParams.SENT_PARAMS;
		eventObj.target = this;
		eventObj.status = success;
		dispatchEvent(eventObj);
	}

	//--------------------------------------------------------------------------------
	/* format of XML data received:
		<widget_instance id="4C7E0112-FF1D-11DB-905A-0030488CBE0D">
		  <widget href="/xml/widgets/343B63CE-FF1D-11DB-905A-0030488CBE0D" id="343B63CE-FF1D-11DB-905A-0030488CBE0D">
			<name>eBay Item Tracker</name>
			<description>Track an eBay auction</description>
			<version>1.0</version>
			<protection protection="none" />
			<mode time="15" mode="timeout" />
			<access access="private" sendable="true" deleteable="true" virtualable="true" />
			<user href="/xml/users/07D11EF6-FCCE-11DB-905A-0030473CBE0D" username="username" id="07D11EF6-FCCE-11DB-906C-000000000" />
			<category name="Shopping" href="/xml/categories/EFC41AA1-2F35-11DB-8753-001372292121" id="EFC5CAD6-2F35-11DB-8753-001372292121" />
			<thumbnail href="/xml/thumbnails/343B63CE-FF1D-127B-905A-0030488CBE0D" contenttype="image/pjpeg" />
			<template href="/xml/templates/343B63CE-FF1D-11DB-815A-0030488CBE0D" contenttype="application/x-shockwave-flash" />
			<movie href="/xml/movies/343B63CE-FF1D-11DB-905A-0052488CBE0D" contenttype="application/x-shockwave-flash" />
			<rating count="0" rating="0.0" />
		  </widget>
		  <profile href="/xml/profiles/2D1E4616-FCDA-11DB-905A-0030488CBE0D" id="2D1E4616-FCDA-11DB-905A-0030488CBE0D" />
		  <access access="private" />
		  <mode time="60" mode="default" />
		  <widget_parameters>
			<widget_parameter id="35265F60-FF44-11DB-BFB0-00146C833363">
			  <name>x_val</name>
			  <value>3434</value>
			</widget_parameter>
			<widget_parameter id="3528E992-FF44-11DB-BFB0-00146C833363">
			  <name>y_val</name>
			  <value>4545</value>
			</widget_parameter>
			<widget_parameter id="352B74C8-FF44-11DB-BFB0-00146C833363">
			  <name>z_val</name>
			  <value>7878</value>
			</widget_parameter>
		  </widget_parameters>
		</widget_instance>
	*/

	public function getWidgetParams()
	{
		_loadedParams = new Object();
		_downloadXML = new XML();
		_downloadXML.ignoreWhite = true;
		_downloadXML.onLoad = Delegate.create(this, gotParams);
		trace("loading params from " + _configURL);
		_downloadXML.load(_configURL);
	}

	//--------------------------------------------------------------------------------
	private function gotParams(success:Boolean) 
	{
		trace("params download complete: " + success);
		trace(_downloadXML.firstChild.childNodes.length + " nodes");
		var widget_parameters_node:XMLNode = XmlUtil.firstChildOfType(_downloadXML.firstChild, 'widget_parameters');
		trace("widget_parameters_node: " + widget_parameters_node.nodeName);
		var widget_params:Array = XmlUtil.childrenOfType(widget_parameters_node,'widget_parameter');
		trace(widget_params.length + " parameters found");
		for (var i:Number=0; i<widget_params.length; i++)
		{
			var widget_param:XMLNode = widget_params[i];
			var key:String = XmlUtil.firstChildOfType(widget_param,'name').firstChild.nodeValue;
			var value = XmlUtil.firstChildOfType(widget_param,'value').firstChild.nodeValue;
			_loadedParams[key] = value;
			//writeln(key + " = " + value);
		}

		var eventObj:Object = new Object();
		eventObj.type = WidgetParams.GOT_PARAMS;
		eventObj.target = this;
		eventObj.status = success;
		eventObj.chumby_params = _loadedParams;
		dispatchEvent(eventObj);
	}
	
	//--------------------------------------------------------------------------------
	public function chumbyParams():Object
	{
		return _loadedParams;
	}
}
